package com.kyrie.lib_sys.application.borrowApplys.controller;

import com.github.pagehelper.IPage;
import com.kyrie.lib_sys.common.entity.PageResult;
import com.kyrie.lib_sys.common.entity.Result;
import com.kyrie.lib_sys.common.pojo.dto.*;
import com.kyrie.lib_sys.common.pojo.entity.Borrow;
import com.kyrie.lib_sys.application.borrowApplys.service.BorrowService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

/**
 * 借阅controller
 *
 * @author heefM
 * @date 2025-09-19
 */
@RestController
@RequestMapping("/borrow/")
@Slf4j
public class BorrowController {

    @Autowired
    private BorrowService borrowService;

    /**
     *  添加借阅
     * @param borrowDTO
     * @return
     */
    @RequestMapping("/add")
    public Result addBorrow(@RequestBody BorrowDTO borrowDTO ) {
        if (log.isInfoEnabled()) {
            log.info("addBorrow: {}",borrowDTO);
        }
        borrowService.insert(borrowDTO);
        return  Result.success("新增图书成功!");
    }

    /**
     *  查询借阅
     * @param borrowDTO
     * @return
     */
    @RequestMapping("/query")
    public Result queryBorrow(@RequestBody BorrowDTO borrowDTO) {
        if (log.isInfoEnabled()) {
            log.info("queryBorrow: {}",borrowDTO);
        }
        Borrow borrow =borrowService.queryBorrow(borrowDTO);
        return Result.success(borrow);
    }

    /**
     *  分页查询借阅
     * @param borrowPageQueryDTO
     * @return
     */
    @RequestMapping("/list")
    public Result<PageResult> queryPage(BorrowPageQueryDTO borrowPageQueryDTO) {
        if (log.isInfoEnabled()) {
            log.info("queryPage: {}",borrowPageQueryDTO);
        }
        if(borrowPageQueryDTO.getPageNum()==null){
            borrowPageQueryDTO.setPageNum(1);
        }
        if(borrowPageQueryDTO.getPageSize()==null){
            borrowPageQueryDTO.setPageSize(10);
        }
        PageResult pageResult = borrowService.queryPage(borrowPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改借阅
     * @param borrowDTO
     * @return
     */
    @RequestMapping("/update")
    public Result updateBorrow(@RequestBody BorrowDTO borrowDTO) {
        if (log.isInfoEnabled()) {
            log.info("updateBorrow: {}",borrowDTO);
        }
        borrowService.update(borrowDTO);
        return Result.success("修改借阅成功!");
    }
    /**
     * 删除借阅
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result deleteBorrow(@RequestParam("id") Long id) {
        if (log.isInfoEnabled()) {
            log.info("deleteBorrow: {}",id);
        }
        borrowService.deleteById(id);
        return Result.success("删除借阅成功!");
    }
    /**
     * 批量删除借阅信息
     * @param ids
     * @return
     */
    @RequestMapping("/deleteBatch")
    public Result deleteBatch(@RequestParam List<Long> ids) {
        if (log.isInfoEnabled()) {
            log.info("批量删除借阅信息deleteBatch: {}", ids);
        }
        borrowService.deleteBatch(ids);
        return Result.success("批量删除借阅信息");
    }
    /**
     * 批量插入借阅信息
     * @param borrowDTOS
     * @return
     */
    @RequestMapping("/insertBatch")
    public Result insertBatch(@RequestBody List<BorrowDTO> borrowDTOS) {
        if (log.isInfoEnabled()) {
            log.info("批量插入借阅信息insertBatch: {}", borrowDTOS);
        }
        borrowService.insertBatch(borrowDTOS);
        return Result.success("批量插入借阅信息");
    }

    /**
     * 图书借阅信息导出
     * @param response
     * @return
     */
    @GetMapping("/excel") // 使用 @GetMapping 或 @RequestMapping 明确请求方法
    public void exportExcel(HttpServletResponse response) {
        // 1. 设置响应内容类型和Header，告诉浏览器这是一个Excel文件需要下载
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=\"borrow_records.xlsx\"");

        // 2. 日志记录
        if (log.isInfoEnabled()) {
            log.info("借阅信息excel报表导出");
        }

        // 3. 调用Service层方法，将HttpServletResponse传递下去
        try {
            borrowService.excelPort(response);
        } catch (IOException e) {
            log.error("导出Excel失败", e);
            // 通常这里可以设置响应状态码为500，但注意不要在抛出IOException后还尝试写响应
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        // 4. 返回类型为void，Spring MVC不会尝试进行视图解析
    }



}
